package com.geeklei.media.ui.menu;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

import com.geeklei.media.ui.R;

import java.util.ArrayList;
import java.util.List;

public class Menu extends FrameLayout {
    private MenuActionListener mActionListener;
    private List<MenuItem> mItems = new ArrayList();
    private ImageView mMask;
    private OnClickListener mViewClickListener = new OnClickListener() {
        public void onClick(View view) {
            if (view instanceof MenuView) {
                MenuItem menuItem = ((MenuView) view).getMenuItem();
                if (menuItem != null && Menu.this.mActionListener != null) {
                    Menu.this.mActionListener.onMenuClick(menuItem);
                }
            }
        }
    };
    private LinearLayout mViewContainer;
    private List<MenuView> mViews = new ArrayList();

    public Menu(Context context) {
        super(context);
        init();
    }

    public Menu(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        init();
    }

    public Menu(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        init();
    }

    private void init() {
        this.mMask = new ImageView(getContext());
        LayoutParams layoutParams = new LayoutParams(-2, -1);
        this.mMask.setLayoutParams(layoutParams);
        this.mMask.setScaleType(ScaleType.FIT_XY);
        this.mMask.setImageResource(R.drawable.play_mask_right_s);
        layoutParams.gravity = Gravity.RIGHT;
        addView(this.mMask);
        this.mViewContainer = new LinearLayout(getContext());
        layoutParams = new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        this.mViewContainer.setOrientation(LinearLayout.VERTICAL);
        layoutParams.gravity = Gravity.RIGHT| Gravity.CENTER_VERTICAL;
        this.mViewContainer.setLayoutParams(layoutParams);
        addView(this.mViewContainer);
    }

    public void setMenuActionListener(MenuActionListener menuActionListener) {
        this.mActionListener = menuActionListener;
    }

    public boolean isEmpty() {
        return this.mItems.size() == 0;
    }

    public void setItems(List<MenuItem> list) {
        this.mItems.clear();
        if (list != null) {
            this.mItems.addAll(list);
        }
        if (this.mItems.size() > 0) {
            this.mMask.setVisibility(VISIBLE);
            setVisibility(VISIBLE);
        } else {
            this.mMask.setVisibility(GONE);
            setVisibility(GONE);
        }
        refreshViews();
    }

    private void refreshViews() {
        int size = this.mItems.size() - this.mViews.size();
        for (int i = 0; i < size; i++) {
            MenuView menuView = (MenuView) LayoutInflater.from(getContext()).inflate(R.layout.vp_menu_view, this.mViewContainer, false);
            this.mViewContainer.addView(menuView);
            menuView.setOnClickListener(this.mViewClickListener);
            this.mViews.add(menuView);
        }
        Log.d("leixun",size+" mViews.size()--"+this.mViews.size());
        size = 0;
        while (size < this.mViews.size()) {
            MenuView menuView = (MenuView) this.mViews.get(size);
            if (size < this.mItems.size()) {
                menuView.setMenuItem((MenuItem) this.mItems.get(size));
                menuView.setVisibility(View.VISIBLE);
                menuView.setLineVisible(size != this.mItems.size() + -1);
            } else {
                menuView.setVisibility(View.GONE);
            }
            size++;
        }
    }
}
